import 'base_model.dart';

/// 陶瓷烧制阶段类型
enum CeramicStageType {
  stage1, // 第一阶段
  stage2, // 第二阶段
  stage3, // 第三阶段
  stage4, // 第四阶段
  stage5, // 第五阶段
  stage6, // 第六阶段
}

/// 陶瓷烧制阶段状态
enum CeramicStageStatus {
  pending, // 待开始
  running, // 进行中
  completed, // 已完成
}

/// 陶瓷烧制阶段模型
class CeramicStage implements BaseModel {
  final int? id;
  final int ceramicId; // 关联的陶瓷作品ID
  final CeramicStageType type; // 阶段类型
  final CeramicStageStatus status; // 阶段状态
  final int durationSeconds; // 持续时间(秒)
  final DateTime? startTime; // 开始时间
  final DateTime? endTime; // 结束时间
  final DateTime createdAt; // 创建时间
  final DateTime updatedAt; // 更新时间

  CeramicStage({
    this.id,
    required this.ceramicId,
    required this.type,
    this.status = CeramicStageStatus.pending,
    this.durationSeconds = 0,
    this.startTime,
    this.endTime,
    required this.createdAt,
    required this.updatedAt,
  });

  @override
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'ceramic_id': ceramicId,
      'type': type.index,
      'status': status.index,
      'duration_seconds': durationSeconds,
      'start_time': startTime?.millisecondsSinceEpoch,
      'end_time': endTime?.millisecondsSinceEpoch,
      'created_at': createdAt.millisecondsSinceEpoch,
      'updated_at': updatedAt.millisecondsSinceEpoch,
    };
  }

  factory CeramicStage.fromJson(Map<String, dynamic> json) {
    return CeramicStage(
      id: json['id'],
      ceramicId: json['ceramic_id'],
      type: CeramicStageType.values[json['type']],
      status: CeramicStageStatus.values[json['status']],
      durationSeconds: json['duration_seconds'],
      startTime:
          json['start_time'] != null
              ? DateTime.fromMillisecondsSinceEpoch(json['start_time'])
              : null,
      endTime:
          json['end_time'] != null
              ? DateTime.fromMillisecondsSinceEpoch(json['end_time'])
              : null,
      createdAt: DateTime.fromMillisecondsSinceEpoch(json['created_at']),
      updatedAt: DateTime.fromMillisecondsSinceEpoch(json['updated_at']),
    );
  }

  // 复制对象并修改指定字段
  CeramicStage copyWith({
    int? id,
    int? ceramicId,
    CeramicStageType? type,
    CeramicStageStatus? status,
    int? durationSeconds,
    DateTime? startTime,
    DateTime? endTime,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return CeramicStage(
      id: id ?? this.id,
      ceramicId: ceramicId ?? this.ceramicId,
      type: type ?? this.type,
      status: status ?? this.status,
      durationSeconds: durationSeconds ?? this.durationSeconds,
      startTime: startTime ?? this.startTime,
      endTime: endTime ?? this.endTime,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }
}
